package cn.lyq.card.ui.customer

import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import cn.lyq.card.R
import cn.lyq.card.base.BaseActivity
import cn.lyq.card.data.model.PageInfo
import cn.lyq.card.data.model.ProblemInfo
import cn.lyq.card.data.src.Result
import cn.lyq.card.data.src.Status
import cn.lyq.card.ui.common.KeyAdapter
import cn.lyq.card.utils.setClick
import com.blankj.utilcode.util.KeyboardUtils
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.act_bank_select.*


class CommonQuestionActivity : BaseActivity() {

    private val mModel by lazy {
        getVM(CustomerVM::class.java)
    }
    private val mAdapter by lazy {
        KeyAdapter()
    }
    private var key = ""
    private val mInfoList = mutableListOf<ProblemInfo>()
    private val page = PageInfo()

    override fun getLayoutId(): Int {
        return R.layout.act_common_question
    }

    override fun initView() {
        super.initView()
        setMyTitle(R.string.str_common_question)

        rv.layoutManager = LinearLayoutManager(this)
        rv.adapter = mAdapter
        mAdapter.setEmptyView(R.layout.layout_empty)
        mAdapter.loadMoreModule.apply {
            checkDisableLoadMoreIfNotFullPage()
            preLoadNumber = 2
            setOnLoadMoreListener {
                getData(page.loadMore())
            }
        }
        mAdapter.setOnItemClickListener { _, _, position ->
            QuestionAnswerActivity.start(this, mInfoList[position])
        }
    }

    override fun initListener() {
        super.initListener()
        refreshLayout.setOnRefreshListener {
            key = etSearch.text.toString()
            getData(page.refresh())
        }
        tvSearch.setClick {
            key = etSearch.text.toString()
            KeyboardUtils.hideSoftInput(etSearch)
            getData(page.refresh())
        }
    }

    private fun getData(pageNum: Int) {
        mModel.getCommonProblem(pageNum, key).observe(this, Observer {
            when (it?.status) {
                Status.SUCCESS -> {
                    loadSuccess(it)
                }
                Status.ERROR -> {
                    page.fail()
                    refreshLayout.isRefreshing = false
                    mAdapter.loadMoreModule.loadMoreFail()
                    ToastUtils.showShort(it.message.getMsg())
                }
                Status.LOADING ->{
                    if (page.isRefresh() && !refreshLayout.isRefreshing){
                        refreshLayout.isRefreshing = true
                    }
                }
            }
        })
    }

    private fun loadSuccess(it: Result<MutableList<ProblemInfo>>) {
        val temp = mutableListOf<String>()
        it.data?.forEach { info ->
            temp.add(info.title ?: "")
        }
        if (page.isRefresh()) {
            mInfoList.clear()
            mAdapter.key = key
            mAdapter.setList(temp)
        } else {
            mAdapter.addData(temp)
        }
        mInfoList.addAll(it.requireData())
        page.success()
        if (temp.size < mModel.pageSize) {
            mAdapter.loadMoreModule.loadMoreEnd()
        } else {
            mAdapter.loadMoreModule.loadMoreComplete()
        }
        refreshLayout.isRefreshing = false
    }


    override fun initObserver() {
        super.initObserver()
        getData(page.refresh())
    }
}